#!/usr/bin/env python

"""
    GameUI.py
    This file contains procedures for handling all Input/Output

    Colors:
    0 - Gray
    1 - Blue
    2 - Green
    3 - Cyan
    4 - Red
    5 - Magenta
    6 - Yellow
    7 - White

    Bolded:
    True - Bright colors
    False - Dark colors

    getKey:
        block:
        True - wait for input (pause game)
        False - do not wait for input

"""

# Imports
import curses

class GameUI(object):
    def __init__(self):
        self.screen = curses.initscr()
        curses.raw()
        curses.noecho()
        curses.cbreak()
        curses.curs_set(0)
        curses.start_color()

        self.screen.keypad(1)
        self.screen.nodelay(1)

        self.init_color_pairs()

        self.key = ''

    def __del__(self):
        self.screen.nodelay(0)
        self.screen.keypad(0)

        curses.curs_set(1)
        curses.nocbreak()
        curses.echo()

        self.screen.erase()
        self.screen.refresh()

        curses.endwin()

    def init_color_pairs(self):
        for foreground in range(1,8):
            curses.init_pair(foreground, foreground, curses.COLOR_BLACK)

    def putChar(self, posy, posx, char, fcolor, bolded = True):
        if fcolor == 0:
            self.screen.addch(posy, posx, char)
        else:
            if bolded:
                self.screen.addch(posy, posx, char, curses.color_pair(fcolor) | curses.A_BOLD)
            else:
                self.screen.addch(posy, posx, char, curses.color_pair(fcolor))
        self.screen.refresh()

    def putString(self, posy, posx, string, fcolor, bolded = True):
        spos = 0
        for s in string:
            self.putChar(posy, posx+spos, s, fcolor, bolded)
            spos+=1

    def getKey(self, block = False):
        if block:
            self.screen.nodelay(0)
            self.key = self.screen.getch()
            self.screen.nodelay(1)
        else:
            key = self.screen.getch()

    def clearLine(self, posy):
        self.screen.move(posy, 0)
        self.screen.clrtoeol()